package com.yungam.other.mes_controller;

import com.google.gson.Gson;
import com.yungam.common.bean.work_order.WorkOrderHistory;
import com.yungam.common.bean.work_order.WorkOrderProductHistory;
import com.yungam.common.service.WorkOrderHistoryService;
import com.yungam.common.service.WorkOrderProductHistoryService;
import com.yungam.other.response.ResData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工单信息同步接口
 */
@Slf4j
@RestController
public class SynchronizeWorkOrderController {

    @Resource
    private WorkOrderHistoryService workOrderHistoryService;
    @Resource
    private WorkOrderProductHistoryService workOrderProductHistoryService;

    @PostMapping("/IMCS/synchronizeWorkOrder")
    public Map<String, Object> synchronizeWorkOrder(@RequestBody Map<String, Object> params) {
        log.info("========= START『mes调用imcs』[工单信息同步接口]=========");
        log.info("=========『mes数据』{}", new Gson().toJson(params));

        //
        List<Map<String, Object>> data = (List<Map<String, Object>>) params.get("data");
        if (data == null) {
            log.error("工单同步失败, 无工单数据");
            return ResData.error("工单同步失败");
        }

        // 查询已有的工单号
        List<String> exist = workOrderHistoryService.list().stream()
                .map(WorkOrderHistory::getWorkOrderNo).collect(Collectors.toList());

        // 待保存的 work_order_product_history 表
        List<WorkOrderProductHistory> wopList = new ArrayList<>();

        for (Map<String, Object> item : data) {
            //
            String workOrderNo = String.valueOf(item.get("workOrderNo")), productNo = String.valueOf(item.get("productNo"));
            if (exist.contains(workOrderNo)) continue;
            List<Map<String, Object>> productSns = (List<Map<String, Object>>) item.get("productProgress");
            // work_order_history 表
            WorkOrderHistory woh = new WorkOrderHistory();
            woh.setWorkOrderNo(workOrderNo);
            woh.setProductNo(productNo);
            woh.setProductName(String.valueOf(item.get("productName")));
            woh.setQty((Integer) item.get("qty"));
            woh.setImportTime(new Date());
            workOrderHistoryService.save(woh);
            //
            for (Map<String, Object> sn : productSns) {
                String productSn = String.valueOf(sn.get("productSn"));
                WorkOrderProductHistory ph = new WorkOrderProductHistory();
                ph.setWorkOrderNo(workOrderNo);
                ph.setProductNo(productNo);
                ph.setProductSn(productSn);
                ph.setDate(new Date());
                ph.setState(0);
                wopList.add(ph);
            }
        }

        // work_order_product_history 表
        if (wopList.size() > 0) {
            for (WorkOrderProductHistory op : wopList) workOrderProductHistoryService.save(op);
        }

        log.info("同步成功");
        return ResData.success("工单同步成功");
    }

}
